package com.btpn.custody.web.m001.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;

import com.btpn.custody.dao.IArea;
import com.btpn.custody.dao.IRegion;
import com.btpn.custody.entity.Area;
import com.btpn.custody.entity.Region;
import com.btpn.custody.service.IGlobalService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.validator.annotations.ExpressionValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;

@ParentPackage(value="custody")
@Results({
	@Result(name="input", location="t_area")
})
@Validations(
	requiredStrings = {
		@RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "areaCode", message = "Area Code must be filled"),
		@RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "areaName", message = "Area Name must be filled")
    },
    expressions = {
        @ExpressionValidator(expression = "regionId != -1", message = "Region must be choose")
	}
)
public class AreaAction extends ActionSupport implements ServletRequestAware, Preparable {
	
	private static final long serialVersionUID = 656529867017245493L;
	
	@Autowired
	private IArea areaDaoHibernate;
	
	@Autowired
	private IGlobalService globalService;
	
	@Autowired
	private IRegion regionDaoHibernate;
	
	private HttpServletRequest request;
	
	private Long regionId;
	private Long areaId;
	private String areaCode;
	private String areaName;
	private Boolean isJakarta;
	
	private List<Area> areas;
	private List<Region> regions;
	private Long _chk[];
	
	@SkipValidation
	@Action(value = "/listArea", results= { @Result(name = "success", location="t_area") })
	public String execute() throws Exception {
		
		setAreaId(0L);
		setRegionId(0L);
		setAreaCode("");
		setAreaName("");
		
		return SUCCESS;
	}
	
	@SkipValidation
	@Action(value = "/editArea", results = { 
			@Result(name="success", location="t_area"),
			@Result(name="error", type="redirectAction", params = {"actionName", "listArea"})
	})
	public String editArea() throws Exception {
		
		String id = getRequest().getParameter("id");
		if (id != null) {
			Area editedArea = areaDaoHibernate.get(Long.parseLong(id));			
			setAreaId(editedArea.getId());
			setAreaCode(editedArea.getCode());
			setAreaName(editedArea.getName());
			setIsJakarta(editedArea.isJakarta());
			setRegionId(editedArea.getRegion().getId());
			
			return SUCCESS;
		} else {
			return ERROR;
		}
	}
	
	@Action(value = "/addOrUpdateArea", results = { 
			@Result(name="success", type="redirectAction", params = {"actionName", "listArea"} )
	})
	public String addOrUpdateArea() throws Exception {
		
		if (areaId == 0L) { 			// create : with validation
			// validate area code
			Area theArea = areaDaoHibernate.getByCode(areaCode);
			if (theArea != null) {
				addFieldError("areaCode", "Area Code must be unique");
				return INPUT;
			}
			
			// validate area name
			theArea = areaDaoHibernate.getByName(areaName);
			if (theArea != null) {
				addFieldError("areaName", "Area Name have been exist");
				return INPUT;
			}
		}
		
		globalService.createOrUpdateArea(areaId, areaCode, areaName, regionId, isJakarta, SecurityContextHolder.getContext().getAuthentication().getName());
		
		return SUCCESS;
	}
	
	@SkipValidation
	@Action(value = "/removeAreas", results = { 
			@Result(name="success", type="redirectAction", params = {"actionName", "listArea"} )
	})
	public String removeAreas() throws Exception {

		globalService.deleteAreas(_chk, SecurityContextHolder.getContext().getAuthentication().getName());
		
		return SUCCESS;
	}
	
	@Override
	public void prepare() throws Exception {
		areas = areaDaoHibernate.findAll();		
		regions = regionDaoHibernate.getAll();
	}
	
	public List<Area> getAreas() {
		return areas;
	}

	public void setAreas(List<Area> areas) {
		this.areas = areas;
	}

	public Long getRegionId() {
		return regionId;
	}

	public void setRegionId(Long regionId) {
		this.regionId = regionId;
	}

	public List<Region> getRegions() {
		return regions;
	}

	public void setRegions(List<Region> regions) {
		this.regions = regions;
	}

	public Long getAreaId() {
		return areaId;
	}

	public void setAreaId(Long areaId) {
		this.areaId = areaId;
	}

	public String getAreaName() {
		return areaName;
	}

	public void setAreaName(String areaName) {
		this.areaName = areaName;
	}

	public Long[] get_chk() {
		return _chk;
	}

	public void set_chk(Long[] _chk) {
		this._chk = _chk;
	}

	public Boolean getIsJakarta() {
		return isJakarta;
	}

	public void setIsJakarta(Boolean isJakarta) {
		this.isJakarta = isJakarta;
	}
	
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public String getAreaCode() {
		return areaCode;
	}

	public void setAreaCode(String areaCode) {
		this.areaCode = areaCode;
	}
}
